<?php
class foody
{
    public function getReviews ($url) 
    {
        //$useragent = "CS_UA: Mozilla/5.0+(X11;+Linux+x86_64)+AppleWebKit/534.14+(KHTML,+like+Gecko;+Google+Web+Preview)+Chrome/9.0.597+Safari/534.14";
        $useragent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0";
        // create a new curl resource
        $ch = curl_init();

        // set URL and other appropriate options
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        // grab URL and pass it to the browser
        $content = curl_exec($ch);

        // close curl resource, and free up system resources
        curl_close($ch);

        return $content;
    }
    
    public function getReviews1 () 
    {
        //return file_get_contents('http://localhost/SVN/my-reply/data/content.html');
        return file_get_contents('http://localhost/SVN/my-reply/data/foody.htm');
    }


    /**
    * Parse content review from API to array
    * @content is content html page review
    * @return $rows is array after parse(image, username, title, des, time)
    */
    public function parseData($content) 
    {
        $rows = array();
        if ($content) {
			preg_match_all ( '@<div\s*class="review-item">(.*)<div\s*class="clear"></div>\s*</div>@siU', $content, $matches );
            
            foreach($matches[0] as $key=>$review) {
                //extract info user (username and image)
                preg_match ('@<a\s*href="(.*)"\s*class="user-link"[^<>]*>\s*<img\s*src="(.*)"\s*/>\s*</a@siU', $review, $infoTag );

                $parseLink = explode('/',$infoTag[1]);
                $rows[$key]['user'] = end($parseLink);
                $rows[$key]['image'] = trim($infoTag[2]);
 
                //extract description
				preg_match ( '@<div\s*class="description">(.*)</div>@siU', $review, $descTag );
                $rows[$key]['description'] = trim($descTag[1]);
                
                //extract username
                preg_match ( '@<div>\s*<a\s*style="font-weight:bold;[^<>]*">(.*)</a>\s*</div>@siU', $review, $usernameTag );
                $rows[$key]['username'] = trim($usernameTag[1]);
                
                //extract time
                preg_match ( '@<div\s*style="font-size:11px;[^<>]*">(.*)</div>@siU', $review, $timeTag );
                $rows[$key]['time'] = trim($timeTag[1]);
                
                //extract point
                preg_match ( '@<div\s*class="review-point">(.*)</div>@siU', $review, $pointTag );
                $rows[$key]['point'] = trim($pointTag[1]);
			}
		}
		
		return $rows;
    }


    /**
    * Parse content review from web to array
    * @content is content html page review
    * @return $rows is array after parse(image, username, title, des, time)
    */
    public function parseDataWeb($content) 
    {
        $rows = array();
        if ($content) {
			preg_match_all ( '@<article\s*class="micro-home-review-item">(.*)</article>@siU', $content, $matches );

            foreach($matches[0] as $key=>$review) {
                //extract info user (username and image)
                preg_match ('@<a\s*href="(.*)"\s*class="user-link"[^<>]*>\s*<img\s*src="(.*)"[^<>]*>@siU', $review, $infoTag );

                //check exists user comment
                if(isset($infoTag[1])) {
                    $parseLink = explode('/',$infoTag[1]);
                    $rows[$key]['user'] = end($parseLink);
                    $rows[$key]['image'] = trim($infoTag[2]);
     
                    //extract description
                    preg_match ( '@<div\s*class="desc\s*disableSection"[^<>]*>(.*)</div>@siU', $review, $descTag );
                    $rows[$key]['description'] = trim($descTag[1]);
                    
                    //extract username
                    preg_match ( '@<a[^<>]*class="user-link">\s*<span[^<>]*>(.*)</span>\s*</a>@siU', $review, $usernameTag );
                    $rows[$key]['username'] = trim(strip_tags($usernameTag[1]));
                    
                    //extract time
                    preg_match ( '@<span\s*class="address"[^<>]*>(.*)</span>@siU', $review, $timeTag );
                    $rows[$key]['time'] = trim($timeTag[1]);
                    
                    //extract point
                    preg_match ( '@<span\s*itemprop="rating">(.*)</span>@siU', $review, $pointTag );
                    $rows[$key]['point'] = trim($pointTag[1]);
                }
			}
		}

		return $rows;
    }
    
    public function filterBadWord() 
    {
    }
}
?>